//
//  BaseAcceptData.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/8/24.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "BaseAcceptData.h"
#import "ByteTool.h"
#import "ResultModel.h"

@implementation BaseAcceptData

+ (BaseAcceptData *)shareManager
{
    static BaseAcceptData *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}


- (instancetype)init{
    
    if (self = [super init]) {
        
        self.travelModel = [[CarTravelModel alloc] init];
        
    }
    return self;
}

+ (AcceptDataType)acceptDataTypeWithData:(NSData *)data{
    NSInteger length = data.length;
    
    Byte byteData[20] = {};
    
    [data getBytes:&byteData length:length];
    
    Byte typeByte = byteData[2];
    
    return typeByte;
}

//是否为反馈数据
+ (BOOL)feedbackWithData:(NSData *)data{
    
    NSInteger length = data.length;
    
    Byte byteData[20] = {};
    
    [data getBytes:&byteData length:length];
    
    Byte typeByte = byteData[0];
    
    
    return typeByte == 0x5A;
    
}


//第四位的反馈
+ (NSInteger)feedbackSuccessWithdata:(NSData *)data{
    
    Byte byteData[20] = {};
    
    [data getBytes:&byteData length:data.length];
    
    Byte success = byteData[3];
    
    return (NSInteger)success;
}


+ (void)analysisData:(NSData *)data{
    
    ResultModel *resultModel = [[ResultModel alloc] init];
    
    if (!data) {
        
        resultModel.code = 100;
        resultModel.message = @"发送数据失败";
        [[BaseAcceptData shareManager] faultSendComplete];
        
       // [LTCSingleHud showMessage:resultModel.message];
        
    }else if (![self feedbackWithData:data]) {
        
        resultModel.code = 2;
        
        resultModel.message = @"数据类型出错";
        [[BaseAcceptData shareManager] faultSendComplete];
         //[LTCSingleHud showMessage:resultModel.message];
        
    }else if([self feedbackWithData:data]){
        
        resultModel.data = data;
        
        AcceptDataType type = [BaseAcceptData acceptDataTypeWithData:data];
        
        resultModel.dataType = type;
        
        NSInteger current = [BaseAcceptData feedbackSuccessWithdata:data];
        
        switch (type) {
                
            case InputPasswordType:
            {
                if ([BaseSendData shareManager].pwComplete) {
                    
                    [BaseSendData shareManager].pwComplete(current);
                    
                }
            }
                break;
            case LockType:{
                
                [BaseSendData shareManager].lockComplete(current);
            }
                break;
            case TravelParamType:{
                
                [[BaseAcceptData shareManager] travelParmasWithData:data];
                
            }
                break;
                
            case LightType:{
                
                [BaseSendData shareManager].lightComplete(current);
                
            }
                break;
            case GeersType:
            {
                [BaseSendData shareManager].greesComplete(current);
            }
                break;
            case ChangePasswordType:{
                
                if ([BaseSendData shareManager].changePwComplete) {
                    
                      [BaseSendData shareManager].changePwComplete(current);
                }
              
            }
                break;
            case AssistanceType:{
                
                [BaseSendData shareManager].assisComplete(current);
            }
                break;
            case NavigateType:{
                
                [BaseSendData shareManager].navComplete(current);
            }
                break;
                
            default:
                break;
        }
        
    }else{
        
        resultModel.code = 100;
        resultModel.message = @"发送数据失败";
       // [LTCSingleHud showMessage:resultModel.message];
        [[BaseAcceptData shareManager] faultSendComplete];
        
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kPeripheraDidReadDataNotification object:resultModel];
    
}

- (void)faultSendComplete{
    
    if ([BaseSendData shareManager].pwComplete) {
        [BaseSendData shareManager].pwComplete(100);
    }
    if ([BaseSendData shareManager].lockComplete) {
        [BaseSendData shareManager].lockComplete(100);
    }
    if ([BaseSendData shareManager].lightComplete) {
        [BaseSendData shareManager].lightComplete(100);
    }
    if ([BaseSendData shareManager].greesComplete) {
        [BaseSendData shareManager].greesComplete(100);
    }
    if ([BaseSendData shareManager].changePwComplete) {
        [BaseSendData shareManager].changePwComplete(100);
    }
    if ([BaseSendData shareManager].assisComplete) {
        [BaseSendData shareManager].assisComplete(100);
    }
    if ([BaseSendData shareManager].navComplete) {
        [BaseSendData shareManager].navComplete(100);
    }
    
}

- (void)travelParmasWithData:(NSData *)data{
    // 5a0f0300 00000000 00000000 c1012e30
    @synchronized (self) {
        
        
        Byte travelByte[14] = {};
        NSInteger length = data.length;
        [data getBytes:&travelByte length:length];
        
        for (int i = 0; i < length; i++) {
            
            NSLog(@"%0x",travelByte[i]);
            
        }
        int a = INT8_C(travelByte[3]);
        
        [BaseAcceptData shareManager].travelModel.power =  a;
        
         Byte s = travelByte[4];
        
        [BaseAcceptData shareManager].travelModel.speed = (s * 0x100 + travelByte[5]) * 0.1 * [SwitcherTool shareManager].rate;
        Byte o = travelByte[6];
        [BaseAcceptData shareManager].travelModel.ODO = (o * 0x100 + travelByte[7]) * 0.1 * [SwitcherTool shareManager].rate;
        Byte d = travelByte[8];
        [BaseAcceptData shareManager].travelModel.DST = (d * 0x100 + travelByte[9]) * 0.1 * [SwitcherTool shareManager].rate;
        
       Byte t = travelByte[10];
        
        [BaseAcceptData shareManager].travelModel.travelTime = (t * 0x100 + travelByte[11]);
        
        Byte stateBits[8] = {};
        [ByteTool byteToBits:stateBits byte:travelByte[12]];
        [BaseAcceptData shareManager].travelModel.isLock = stateBits[7];
        [BaseAcceptData shareManager].travelModel.isChage = stateBits[6];
        [BaseAcceptData shareManager].travelModel.islightONOFF = stateBits[5];
        [BaseAcceptData shareManager].travelModel.islightAUTO = stateBits[4];
        [BaseAcceptData shareManager].travelModel.mode = stateBits[3];
        [BaseAcceptData shareManager].travelModel.isXH = stateBits[2];
        [BaseAcceptData shareManager].travelModel.faultByte = travelByte[13];
        
    }
    
    
}


@end
